package service

import (
	"context"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _categoryService struct{}

func (s _categoryService) GetByIdApi(ctx context.Context, enterpriseId int64, id int64) (*model.CategoryEntity, error) {
	return dao.CategoryEntity.WithContext(ctx).Where(dao.CategoryEntity.EnterpriseID.Eq(enterpriseId), dao.CategoryEntity.ID.Eq(id)).First()
}

func (s _categoryService) GetByParentIdApi(ctx context.Context, parentId int64) (*model.CategoryEntity, error) {
	return dao.CategoryEntity.WithContext(ctx).Where(dao.CategoryEntity.ParentID.Eq(parentId)).First()
}
func (s _categoryService) ModifyStatusByIdApi(ctx context.Context, id, status int64) error {
	if _, err := dao.CategoryEntity.WithContext(ctx).Where(dao.CategoryEntity.ID.Eq(id)).UpdateSimple(
		dao.CategoryEntity.Status.Value(status),
	); err != nil {
		return err
	}
	return nil
}

func (s _categoryService) GetByNameApi(ctx context.Context, enterpriseId int64, name string) (*model.CategoryEntity, error) {
	return dao.CategoryEntity.WithContext(ctx).Where(dao.CategoryEntity.EnterpriseID.Eq(enterpriseId), dao.CategoryEntity.Name.Eq(name)).First()
}

func (s _categoryService) CreateApi(ctx context.Context, m *model.CategoryEntity) error {
	if err := dao.CategoryEntity.WithContext(ctx).Create(m); err != nil {
		return err
	}
	return nil
}

var CategoryService = new(_categoryService)
